package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.daily.dto.carriage.DailyTrainCarriageAddOrEditDTO;
import com.train.daily.dto.carriage.DailyTrainCarriagePageDTO;
import com.train.daily.dto.carriage.DailyTrainCarriageQueryListDTO;
import com.train.daily.po.DailyTrainCarriagePO;
import com.train.daily.resp.carriage.DailyTrainCarriagePageResp;
import com.train.daily.resp.carriage.DailyTrainCarriageQueryListResp;
import com.train.mybatis.base.MyBaseMapper;

import java.time.LocalDate;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrainCarriage(每日车厢)】的数据库操作Mapper
 * @createDate 2025-03-01 21:20:49
 * @Entity generator.domain.DailyTrainCarriage
 */
public interface DailyTrainCarriageMapper extends MyBaseMapper<DailyTrainCarriagePO> {

    default PageResp<DailyTrainCarriagePageResp> pageList(DailyTrainCarriagePageDTO dailyTrainCarriagePageDTO) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(dailyTrainCarriagePageDTO.getDate()), DailyTrainCarriagePO::getDate, dailyTrainCarriagePageDTO.getDate())
                .like(StrUtil.isNotEmpty(dailyTrainCarriagePageDTO.getCode()), DailyTrainCarriagePO::getTrainCode, dailyTrainCarriagePageDTO.getCode());
        PageResp<DailyTrainCarriagePO> dailyTrainCarriagePOPageResp = this.selectPage(
                dailyTrainCarriagePageDTO,
                queryWrapper, new PageSorted(DailyTrainCarriagePO::getUpdateTime), new PageSorted(DailyTrainCarriagePO::getCreateTime));
        List<DailyTrainCarriagePageResp> dailyTrainCarriagePageRespList = BeanUtil.copyToList(dailyTrainCarriagePOPageResp.getData(), DailyTrainCarriagePageResp.class);
        return new PageResp<>(dailyTrainCarriagePOPageResp.getTotal(), dailyTrainCarriagePageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainCarriagePO::getId, id);
        return this.exists(queryWrapper);
    }

    default boolean existsDailyTrainCarriage(DailyTrainCarriageAddOrEditDTO dailyTrainCarriageAddOrEditDTO) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainCarriagePO::getDate, dailyTrainCarriageAddOrEditDTO.getDate())
                .eq(StrUtil.isNotEmpty(dailyTrainCarriageAddOrEditDTO.getTrainCode()), DailyTrainCarriagePO::getTrainCode, dailyTrainCarriageAddOrEditDTO.getTrainCode())
                .eq(ObjectUtil.isNotNull(dailyTrainCarriageAddOrEditDTO.getIndex()), DailyTrainCarriagePO::getIndex, dailyTrainCarriageAddOrEditDTO.getIndex());
        return this.exists(queryWrapper);
    }

    default List<DailyTrainCarriageAddOrEditDTO> getIdCodeList(DailyTrainCarriageAddOrEditDTO dailyTrainCarriageAddOrEditDTO) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DailyTrainCarriagePO::getId, DailyTrainCarriagePO::getIndex)
                .eq(DailyTrainCarriagePO::getTrainCode, dailyTrainCarriageAddOrEditDTO.getTrainCode())
                .eq(DailyTrainCarriagePO::getDate, dailyTrainCarriageAddOrEditDTO.getDate());
        List<DailyTrainCarriagePO> trainCarriagePOList = this.selectList(queryWrapper);
        return BeanUtil.copyToList(trainCarriagePOList, DailyTrainCarriageAddOrEditDTO.class);
    }

    default int deleteTrainCarriages(LocalDate parse, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainCarriagePO::getDate, parse)
                .in(DailyTrainCarriagePO::getTrainCode, trainCodes);
        return this.delete(queryWrapper);
    }

    default boolean existsDailyTrainCarriage(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainCarriagePO::getDate, date)
                .in(DailyTrainCarriagePO::getTrainCode, trainCodes)
                .last("limit 1");
        return this.exists(queryWrapper);
    }

    default List<DailyTrainCarriageQueryListResp> selectTrainCarriageList(DailyTrainCarriageQueryListDTO dto) {
        LambdaQueryWrapper<DailyTrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DailyTrainCarriagePO::getColCount,
                        DailyTrainCarriagePO::getRowCount,
                        DailyTrainCarriagePO::getSeatCount,
                        DailyTrainCarriagePO::getIndex)
                .eq(DailyTrainCarriagePO::getDate, dto.getDate())
                .eq(DailyTrainCarriagePO::getTrainCode, dto.getTrainCode())
                .eq(DailyTrainCarriagePO::getSeatType, dto.getSeatTypeCode())
                .orderBy(true, true, DailyTrainCarriagePO::getIndex);
        List<DailyTrainCarriagePO> dailyTrainCarriagePOS = this.selectList(queryWrapper);
        return BeanUtil.copyToList(dailyTrainCarriagePOS, DailyTrainCarriageQueryListResp.class);
    }
}